package cn.com.api.controller;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.api.entity.ApiCash;
import cn.com.api.entity.ApiShop;
import cn.com.api.entity.ApiShopTodayStats;
import cn.com.api.entity.ApiSmslog;
import cn.com.api.entity.ApiT_shops_time;
import cn.com.api.service.ApiConllectionService;
import cn.com.api.service.ApiDianPingService;
import cn.com.api.service.ApiShopsService;
import cn.com.api.service.ApiShopsTimeService;
import cn.com.api.service.ApiSmslogService;
import cn.com.api.service.ApiTypeService;
import cn.com.api.service.CheckService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "shops/")
public class ApiShopsController {
	@Autowired
	private ApiShopsService apiShopsService;
	@Autowired
	private ApiSmslogService apiSmslogService;
	@Autowired
	private ApiConllectionService conllectService;
	@Autowired
	private ApiShopsTimeService apiShopsTimeService;
	@Autowired
	private ApiDianPingService apiDianPingService;
	@Autowired
	private ApiTypeService apiTypeService;
	@Autowired
	private CheckService checkService;

	private static Logger logger = Logger.getLogger(ApiT_logController.class);

	/**
	 * 登录
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "login")
	public void login(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String cUname = request.getParameter("cUname");
		String cPass = request.getParameter("cPass");
		String deviceId = request.getParameter("deviceId");// 设备id
		String deviceType = request.getParameter("deviceType");// 设备类型：0:IOS
																// 1:Android
		Map<String, Object> map = new HashMap<String, Object>();
		if (cUname != null && !"".equals(cUname.trim()) && cPass != null && !"".equals(cPass)) {
			ApiShop shop = new ApiShop();
			shop.setcUname(cUname);
			shop.setcPass(CipherUtil.generatePassword(cPass));
			if (!Util.isNull(deviceType)) {
				shop.setDeviceid(deviceId);
				shop.setDevicetype(Integer.parseInt(deviceType));
			}
			ApiShop apiShop = null;
			apiShop = apiShopsService.login(shop);
			if (apiShop != null) {
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
				String[] stime = apiShop.getStime() != null ? apiShop.getStime().split(",") : new String[0];
				String[] etime = apiShop.getEtime() != null ? apiShop.getStime().split(",") : new String[0];
				String setime = "";
				for (int i = 0; i < stime.length; i++) {
					setime += stime[i] + "~" + etime[i] + ",";
				}
				apiShop.setcHours(setime);
				if (apiTypeService.checkIsFoodTakeAway(apiShop.getcType()) == 1) {// 美食外卖
					apiShop.setIsEdit(1);
				} else {
					apiShop.setIsEdit(0);
				}
				// 将用户信息返回
				map.put("datas", apiShop);
			} else {
				map.put(Utils.RESULT, "0002");
				map.put(Utils.MESSAGE, "用户名或密码不正确！");// 用户名或密码不正确！
				map.put("datas", "");
			}
		} else {
			map.put(Utils.RESULT, "0008");
			map.put(Utils.MESSAGE, "用户名或密码不正确！");// 用户名或密码不能为空！
			map.put("datas", "");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		// out.write(CipherUtil.generatePassword(cPass));
		out.close();

	}

	/**
	 * 修改密码
	 */
	@RequestMapping(value = "modifyPass")
	public void modifyPass(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String cMob = request.getParameter("cMob");
		String code = request.getParameter("code");
		String cPass = request.getParameter("cPass");
		String cPassTwo = request.getParameter("cPassTwo");
		Map<String, Object> map = new HashMap<String, Object>();
		if (cPass != null && !"".equals(cPass) && cPass.equals(cPassTwo)) {

			ApiSmslog apiSmslog = new ApiSmslog();
			apiSmslog.setcTel(cMob);
			apiSmslog.setCode(code);

			if (apiSmslogService.checkCode(apiSmslog)) {
				ApiShop shop = apiShopsService.findShopByCmobi(cMob);
				if (shop != null) {
					shop.setcPass(CipherUtil.generatePassword(cPass));
					apiShopsService.modifyPass_shops(shop);
					apiSmslogService.updateSmslog(apiSmslog);
					map.put("datas", shop);
					map.put(Utils.RESULT, "0000");
					map.put(Utils.MESSAGE, "修改成功！");
				}
			} else {
				map.put(Utils.RESULT, "0002");
				map.put(Utils.MESSAGE, "验证码不匹配，请重新填写！");
			}
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 设备id type 更新存储----用户端、配送端可用
	 */
	@RequestMapping(value = "upateShopDevice")
	public void upateShopDevice(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String shop_id = request.getParameter("shop_id");
		String deviceId = request.getParameter("deviceId");// 设备id
		String deviceType = request.getParameter("deviceType");// 设备类型：0:IOS
																// 1:Android
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (!Util.isNull(shop_id)) {
				ApiShop shop = apiShopsService.findT_shopsById(Integer.parseInt(shop_id));
				if (shop != null) {
					shop.setDeviceid(deviceId);
					shop.setDevicetype(Integer.parseInt(deviceType));
					apiShopsService.updateT_shops(shop);
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "操作成功");
				}
			} else {
				map.put(Utils.RESULT, "0002");
				map.put(Utils.MESSAGE, "用户名为空！");
			}

		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	// 商铺详情
	@RequestMapping("/getShopsById")
	public void getShopsById(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String sid1 = request.getParameter("sid");
		Integer sid = Integer.parseInt(sid1);

		try {
			List<ApiShop> list = apiShopsService.getShops(sid);
			if (list.size() >= 1) {
				map.put("datas", list);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "查询成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "查询不到相关数据！");
			}

		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		//
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	// 根据类型获取商店列表
	@RequestMapping("/searchShopsByType")
	public void searchShopsByType(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String cType = request.getParameter("cType");
		String city_id = request.getParameter("city_id");
		String pageCount = request.getParameter("pageCount");
		String searchBy = request.getParameter("searchBy");
		String longitude = request.getParameter("longitude");// 经度
		String latitude = request.getParameter("latitude");// 纬度
		// searchBy 表示是 1.智能排序、2.点评最多、3.销量最好、4.价格最低、5.距离最近

		try {
			Map<String, String> params = new HashMap<String, String>();
			params.put("cType", cType);
			params.put("city_id", city_id);
			params.put("pageCount", pageCount);
			params.put("longitude", longitude);
			params.put("latitude", latitude);
			params.put("searchBy", "5");
			apiShopsService.searchShopsByType(params, map);
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 生活服务--开锁
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/searchGoodsByShopId")
	public void searchGoodsByShopId(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			String shop_id = request.getParameter("shop_id");
			String pageCount = request.getParameter("pageCount");
			Map<String, String> params = new HashMap<String, String>();
			params.put("shop_id", shop_id);
			params.put("pageCount", pageCount);

			apiShopsService.searchGoodsByShopId(params, map);

		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();

	}

	/**
	 * 生活服务--附近门店
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/nearbyShops")
	public void nearbyShops(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			// 传city_id 大的分类 cCode(生活服务)
			String city_id = request.getParameter("city_id");
			String cCode = request.getParameter("cCode");
			String pageCount = request.getParameter("pageCount");
			String longitude = request.getParameter("longitude");// 经度
			String latitude = request.getParameter("latitude");// 纬度
			Map<String, String> params = new HashMap<String, String>();
			params.put("city_id", city_id);
			params.put("cCode", cCode);
			params.put("pageCount", pageCount);
			params.put("longitude", longitude);
			params.put("latitude", latitude);

			apiShopsService.searchNearbyShops(params, map);

		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 根据id查询/店铺详情
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getShopsByCmobi")
	public void getShopsByCmobi(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String c_mobi = request.getParameter("c_mobi");
		ApiShop shop = apiShopsService.findShopByCmobi(c_mobi);
		response.setCharacterEncoding("UTF-8");

		PrintWriter out = response.getWriter();
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put("datas", shop);
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 用户忘记密码
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "forgetPwd")
	public void forgetPwd(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String cMob = request.getParameter("cMob");// 用户名-手机号
		String code = request.getParameter("code");// 验证码
		String cPass = request.getParameter("cPass");// 新密码
		String cPassTwo = request.getParameter("cPassTwo");// 确认密码
		Map<String, Object> map = new HashMap<String, Object>();
		if (cPass != null && !"".equals(cPass) && cPass.equals(cPassTwo)) {
			// 忘记密码接口，先查询用户，更新用户密码信息，更新验证码信息 返回用户信息
			ApiShop shop = apiShopsService.findShopByCmobi(cMob);
			// shop.setCpass(CipherUtil.generatePassword(cPass));//设置新密码
			Date now = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			// shop.setClogdate(dateFormat.format(now));//不需要再重新登录
			ApiSmslog apiSmslog = new ApiSmslog();
			apiSmslog.setcTel(cMob);
			apiSmslog.setCode(code);
			// 注册
			// 校验验证码是否正确
			if (apiSmslogService.checkCode(apiSmslog)) {
				apiShopsService.forgetPwd(shop, apiSmslog);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
				map.put("datas", shop);
			} else {
				map.put(Utils.RESULT, "0002");
				map.put(Utils.MESSAGE, "验证码不匹配，请重新填写！");
			}

		} else {
			map.put(Utils.RESULT, "0002");
			map.put(Utils.MESSAGE, "两次密码不一致，请重新填写！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * update
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "updateT_shops")
	public void updateT_shops(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String c_id = request.getParameter("c_id");
		String is_open = request.getParameter("is_open");

		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (is_open != null && !"".equals(is_open.trim())) {
				ApiShop t_shop = new ApiShop();
				// 获取原数据
				t_shop = apiShopsService.findT_shopsById(Integer.valueOf(c_id));
				// 修改营业状态
				t_shop.setIs_open(Integer.valueOf(is_open));
				//
				apiShopsService.updateT_shops(t_shop);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "编辑成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "检查失败！");
			}
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 修改商铺营业状态
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "updateShop_status")
	public void updateShop_status(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String c_id = request.getParameter("c_id");
		String is_open = request.getParameter("is_open");

		Map<String, Object> map = new HashMap<String, Object>();

		if (is_open != null && !"".equals(is_open.trim())) {
			ApiShop t_shop = new ApiShop();
			// 获取原数据
			t_shop = apiShopsService.findT_shopsById(Integer.valueOf(c_id));
			// 修改营业状态
			t_shop.setIs_open(Integer.valueOf(is_open));
			//
			apiShopsService.updateT_shops(t_shop);

			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "修改成功！");
		} else {
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "修改失败！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 修改商家公告
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "updateShop_ad")
	public void updateShop_ad(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String c_id = request.getParameter("c_id");
		String c_ad = request.getParameter("c_ad");
		c_ad = new String(c_ad.getBytes("ISO-8859-1"), "utf-8");

		Map<String, Object> map = new HashMap<String, Object>();

		if (c_ad != null && !"".equals(c_ad.trim())) {
			ApiShop t_shop = new ApiShop();
			// 获取原数据
			t_shop = apiShopsService.findT_shopsById(Integer.valueOf(c_id));
			// 修改商家公告
			t_shop.setcAd(c_ad);
			//
			apiShopsService.updateT_shops(t_shop);

			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "修改成功！");
		} else {
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "修改失败！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 修改商铺电话
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "updateShop_tel")
	public void updateShop_tel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String c_id = request.getParameter("c_id");
		String c_tel = request.getParameter("c_tel");

		Map<String, Object> map = new HashMap<String, Object>();

		if (c_tel != null && !"".equals(c_tel.trim())) {
			ApiShop t_shop = new ApiShop();
			// 获取原数据
			t_shop = apiShopsService.findT_shopsById(Integer.valueOf(c_id));
			// 修改联系电话
			t_shop.setcTel(c_tel);
			//
			apiShopsService.updateT_shops(t_shop);

			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "修改成功！");
		} else {
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "修改失败！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 修改 区域，地址
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "updateShop_addr")
	public void updateShop_addr(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String c_id = request.getParameter("c_id");
		String c_address = request.getParameter("c_address");
		// String c_uname = request.getParameter("c_uname");
		String c_area = request.getParameter("c_area");

		Map<String, Object> map = new HashMap<String, Object>();

		if (c_address != null && !"".equals(c_address.trim())) {
			ApiShop t_shop = new ApiShop();
			// 获取原数据
			t_shop = apiShopsService.findT_shopsById(Integer.valueOf(c_id));
			// 修改用户名， 区域，地址
			t_shop.setcAddress(c_address);
			t_shop.setcArea(c_area);
			// t_shop.setcUname(c_uname);
			//
			apiShopsService.updateT_shops(t_shop);

			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "修改成功！");
		} else {
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "修改失败！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	// 搜索新加↓↓↓↓↓↓↓↓↓↓↓↓↓↓
	@RequestMapping(value = "searchShopByCondition")
	public void searchShopByCondition(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// searchBy 表示是 1.智能排序、2.点评最多、3.销量最好、4.价格最低、5.距离最近
		Map<String, Object> map = new HashMap<String, Object>();
		String cType = request.getParameter("cType");
		String city_id = request.getParameter("city_id");
		String pageCount = request.getParameter("pageCount");
		String searchBy = request.getParameter("searchBy");
		String longitude = request.getParameter("longitude");// 经度
		String latitude = request.getParameter("latitude");// 纬度
		// searchBy 表示是 1.智能排序、2.点评最多、3.销量最好、4.价格最低、5.距离最近

		try {
			Map<String, String> params = new HashMap<String, String>();
			params.put("cType", cType);
			params.put("city_id", city_id);
			params.put("pageCount", pageCount);
			params.put("longitude", longitude);
			params.put("latitude", latitude);
			params.put("searchBy", searchBy);
			apiShopsService.searchShopsByType(params, map);
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	// ↑↑↑↑↑↑↑↑↑↑↑↑↑↑

	@RequestMapping(value = "getShopOrderAccount")
	public void getShopOrderAccount(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String shop_id = request.getParameter("shop_id");
		Map<String, Object> map = new HashMap<String, Object>();

		try {

			ApiShopTodayStats stats = apiShopsService.getShopOrderAccount(Integer.valueOf(shop_id));
			map.put("datas", stats);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}

		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	@RequestMapping(value = "updateShopTel")
	public @ResponseBody Map<String, Object> updateShopTel(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String shop_id = request.getParameter("shop_id");
		String tel = request.getParameter("tel");
		Map<String, Object> res = new HashMap<String, Object>();

		try {
			if (apiShopsService.updateTel(tel, shop_id)) {
				res.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				res.put(Utils.MESSAGE, "修改成功");
			} else {
				res.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				res.put(Utils.MESSAGE, "修改失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			res.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			res.put(Utils.MESSAGE, "系统异常");
		}
		return res;
	}

	@RequestMapping(value = "updateToppedTime")
	public @ResponseBody Map<String, Object> updateToppedTime(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> res = new HashMap<String, Object>();
		String shop_id = request.getParameter("shop_id");
		String business = request.getParameter("business");
		String stime = request.getParameter("stime");
		String etime = request.getParameter("etime");
		String token = request.getParameter("token");
		try {
			if (checkService.checkToken(shop_id, token, res)) {
				ApiT_shops_time ast = new ApiT_shops_time();
				ast.setBusiness_type(business);
				ast.setStart_time(stime);
				ast.setEnd_time(etime);
				ast.setShop_id(Integer.parseInt(shop_id));

				apiShopsTimeService.updateShopTime(ast, res);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			res.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			res.put(Utils.MESSAGE, "系统异常");
		}
		return res;
	}

	@RequestMapping(value = "shopInfo")
	public @ResponseBody Map<String, Object> shopInfo(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> res = new HashMap<String, Object>();
		try {
			Map<String, Object> shop = new HashMap<String, Object>();
			String shopid = request.getParameter("shopid");
			shop = apiShopsService.findShopByShopId(shopid);
			Map<String, Object> ret = new HashMap<String, Object>();
			ret.put("cash_money", shop.get("cash_money"));
			ret.put("pre_payment", shop.get("pre_payment"));
			ret.put("balance", shop.get("balance"));
			ret.put("bank_no", shop.get("bank_no"));
			ret.put("bank_name", shop.get("bank_name"));
			ret.put("accept_name", shop.get("accept_name"));
			ret.put("accept_phone", shop.get("accept_phone"));
			ret.put("least_money", shop.get("least_money"));
			res.put("datas", ret);
			res.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			res.put(Utils.MESSAGE, "查询成功");
		} catch (Exception e) {
			e.printStackTrace();
			res.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			res.put(Utils.MESSAGE, "系统异常");
		}
		return res;
	}

	@RequestMapping(value = "shopCash")
	public @ResponseBody Map<String, Object> shopCash(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> res = new HashMap<String, Object>();
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = new Date();
			String shopid = request.getParameter("shopid");
			String money = request.getParameter("money");
			ApiCash apiCash = new ApiCash();
			apiCash.setTime(format.format(date));
			apiCash.setNum(date.getTime() + shopid);
			apiCash.setMoney(money);
			apiCash.setShopid(Integer.parseInt(shopid));
			if (apiShopsService.shopCash(apiCash)) {
				res.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				res.put(Utils.MESSAGE, "申请成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			res.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			res.put(Utils.MESSAGE, "系统异常");
		}
		return res;
	}

	@RequestMapping(value = "shopStat")
	public @ResponseBody Map<String, Object> shopStat(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String shopid = request.getParameter("shopid");
		return apiShopsService.shopStat(shopid);
	}

	@RequestMapping(value = "shopBills")
	public @ResponseBody Map<String, Object> shopBills(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String shopid = request.getParameter("shopid");
		return apiShopsService.shopBills(shopid);
	}

	@RequestMapping(value = "cashDetail")
	public @ResponseBody Map<String, Object> cashDetail(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String cashid = request.getParameter("cashid");
		String token = request.getParameter("token");
		return apiShopsService.cashDetail(cashid, token);
	}

	@RequestMapping(value = "statDetail")
	public @ResponseBody Map<String, Object> statDetail(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String statid = request.getParameter("statid");
		String shopid = request.getParameter("shopid");
		String token = request.getParameter("token");
		return apiShopsService.statDetail(statid, shopid, token);
	}

	@RequestMapping(value = "updateIsOpen")
	public @ResponseBody Map<String, Object> updateIsOpen(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String shopid = request.getParameter("shopid");
		String open = request.getParameter("isopen");
		String token = request.getParameter("token");
		return apiShopsService.updateIsOpen(open, shopid);
	}

}
